package org.tale.cloudbase.controller;

import com.alibaba.nacos.api.exception.NacosException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.tale.cloudbase.service.BaseFeignService;

import javax.annotation.Resource;

/**
 * @program: boot-base
 * @author: tale-mi
 * @description:
 * @create: 2021/4/27 17:35
 **/
@RestController
@RequestMapping("consumer")
public class ConsumerController {
    @Autowired
    private RestTemplate restTemplate;

    @Resource
    private BaseFeignService baseFeignService;
    
    @Value("${base.service-url.nacos-cloud-base-service}")
    private String url;
    
    @RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)
    public String echo(@PathVariable String str) throws NacosException {
        
        String result=restTemplate.getForObject(url+"/"+str, String.class);
        System.out.println(String.format("请求URL:%s,响应结果:%s", url, result));
        return result;
    }

    @RequestMapping(value = "/echofeign/{str}", method = RequestMethod.GET)
    public String echoFeign(@PathVariable String str) throws NacosException {
        String result=baseFeignService.feignEcho(str);
        System.out.println(String.format("请求URL:%s,响应结果:%s", url, result));
        return result;
    }
    
    //该方法，无法使用，因为未使用loadloadbalancer，所以会报UnknownHostException: nacos-cloud-base
/*    @RequestMapping(value = "/echoHutool/{str}", method = RequestMethod.GET)
    public String echoHutool(@PathVariable String str) throws NacosException {

        String result2 = HttpRequest.get(url+"/"+str).execute().body();
        System.out.println(String.format("请求URL:%s,响应结果:%s", url, result2));
        return result2;
    }*/
}
